<?php

namespace app\backend\controller;

use app\backend\service\AppAppContentService;
use app\backend\service\AppContentService;

class AppContent extends Backend
{
    /**
     * 初始化
     */
    public function initialize()
    {
        parent::initialize();
        cache('contentType' . adminInfo('id'), 2, 3600 * 4);
    }

    /**
     * 文章列表
     * @param AppContentService $service
     * @return string|\think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index(AppContentService $service)
    {
        if ($this->request->isAjax()) {
            return $this->success($service->lists($this->request->param()));
        }
        return $this->fetch('', $service->contentLabel());
    }

    /**
     * 添加操作
     * @param AppContentService $service
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(AppContentService $service)
    {
        if ($this->request->isAjax()) {
            $renewResult = $service->add($this->request->param());
            return $this->success($renewResult);
        }
        // 模板渲染
        return $this->fetch('', $service->contentLabel());
    }

    /**
     * 审核操作
     * @param AppContentService $service
     * @return string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit(AppContentService $service)
    {
        // 请求参数
        $params = $this->request->param();
        // 审核操作
        if ($this->request->isAjax()) {
            $renewResult = $service->edit($params);
            return $this->success($renewResult);
        }
        // 模板渲染
        $fetchData = $service->contentLabel();
        $fetchData['info'] = $service->detail($params['id']);
        return $this->fetch('', $fetchData);
    }

    /**
     * 文章详情
     * @param AppContentService $service
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(AppContentService $service)
    {
        // 模板渲染
        $fetchData = $service->contentLabel();
        $fetchData['info'] = $service->detail($this->request->param('id'));
        return $this->fetch('', $fetchData);
    }

    public function del(AppContentService $service)
    {
        return $this->success($service->del($this->request->param('id')));
    }
}